package com.team5101.client.handler;


import com.alibaba.fastjson.JSONObject;
import com.team5101.client.RpcConsumer;
import com.team5101.model.RpcRequest;
import com.team5101.service.DemoService;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Callable;


@ChannelHandler.Sharable
public class DemoClientHandler extends SimpleChannelInboundHandler<String> {

    private Logger logger = LoggerFactory.getLogger(DemoClientHandler.class);

    /**
     * 收到服务器端的响应,
     *
     * @param ctx
     * @param msg
     * @throws Exception
     */
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        //{"isSuccess":true,"requestId":"e214b51b-ab52-4e58-b406-a734a011b69e","result":"success"}

        RpcConsumer.getDataMap().put(JSONObject.parseObject(msg).getString("requestId"), msg);
        logger.info("dataMap：{}", RpcConsumer.getDataMap());

    }
}


//public class DemoClientHandler extends ChannelInboundHandlerAdapter implements Callable {
////public class DemoClientHandler extends ChannelInboundHandlerAdapter implements Callable {
//
//	private ChannelHandlerContext channelHandlerContext;
//
//	private RpcRequest para;
//
//	private Object result;
//
//	@Override
//	public synchronized void channelActive(ChannelHandlerContext ctx) throws Exception {
//		this.channelHandlerContext = ctx;
//	}
//
//	/**
//	 * 写出数据，等待唤醒
//	 *
//	 * @return
//	 * @throws Exception
//	 */
//	@Override
//	public synchronized Object call() throws Exception {
//		channelHandlerContext.writeAndFlush(this.para);
//		wait();
//		return result;
//	}
//
//
//	/**
//	 * 收到服务器端低响应,唤醒等待线程
//	 *
//	 * @param ctx
//	 * @param msg
//	 * @throws Exception
//	 */
//	@Override
//	public synchronized void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//		this.result = msg;
//		notify();
//	}
//
//	/**
//	 * 设置参数
//	 *
//	 * @param para
//	 */
//	public synchronized void setPara(RpcRequest para) {
//		this.para = para;
//	}
//
//}
